<?php 
	require_once('./modules/config.inc');
	
	class Util
	{
		static $tagsRegex;
		
		static function cleanTags($text)
		{
			if( strlen(Util::$tagsRegex) == 0)
			{				
				$config = Config::getInstance();
				
				$forbidden = $config->get("./forbiddenTags");

				$tagArray = explode(",",$forbidden);
				
				Util::$tagsRegex = "/";
				
				foreach($tagArray as $tag)
				{
					$tag = trim((string)$tag);
					if(strlen($tag) == 0)
						continue;
						
					if( Util::$tagsRegex != "/")
						Util::$tagsRegex .= "|";
					Util::$tagsRegex .= "<".$tag.".*?>|<\/".$tag.">";
				}
				
				Util::$tagsRegex .= "/ims";
			}
						
			return preg_replace(Util::$tagsRegex," ",$text);
		}
		
		static function cutStringFullWords($str,$len)
		{
			$arr = explode(" ",$str);

			$result = "";
			$count=0;
			while(strlen($result) < $len && $count < count($arr))
			{
				$val = trim($arr[$count]);
				$result .= $val." ";
				$count++;
			}
			return rtrim($result);
		}
		
		static function getPaging($start,$itemsCount,$pageTypeStr)
		{
			global $G_HOME;
			
			$itemsInPage = Config::getInstance()->get('./maxItemsPerPage');

			$result = "<div class='pagingBar'>";
			
			$page = $start/$itemsInPage+1;
			$numOfPages = floor($itemsCount/$itemsInPage);
			
			if($itemsCount%$itemsInPage > 0)
				$numOfPages += 1;

			if($page + 4 < 10)
			{
				$startPage = 1;
				$endPage = min(10,$numOfPages);
			}
			else if($page + 4 > $numOfPages)
			{
				$startPage = max(1,$numOfPages-9);
				$endPage = $numOfPages;
			}
			else
			{
				$startPage = $page-5;
				$endPage = min($page+4,$numOfPages);
			}
			
			if( $start > 0)//add previous button.
				$result .= "<a href='".Util::combinePath($G_HOME,"index.php?".$pageTypeStr."&amp;itemId=".($start-$itemsInPage))."'>&nbsp;&#171;Previous&nbsp;</a>";
			else
				$result .= "<label class='disabledPaging'>&nbsp;&#171;Previous&nbsp;</label>";
			
			for($i=$startPage;$i<=$endPage;$i++)
			{
				if($i == $page)
					$result .= "<label class='currPage'>&nbsp;".$i."&nbsp;</label>";
				else
					$result .= "<a href='".Util::combinePath($G_HOME,"index.php?".$pageTypeStr."&amp;itemId=".(($i-1)*$itemsInPage))."'>&nbsp;".$i."&nbsp;</a>";
			}

			if($page < $numOfPages)
				$result .= "<a href='".Util::combinePath($G_HOME,"index.php?".$pageTypeStr."&amp;itemId=".($start+$itemsInPage))."'>&nbsp;Next&#187;&nbsp;</a>";
			else
				$result .= "<label class='disabledPaging'>&nbsp;Next&#187;&nbsp;</label>";
					
			$result .="</div>";
			
			return $result;
		}
		
		public static function safeGetPost($varName)
		{
			$res = filter_input(INPUT_POST , $varName, FILTER_SANITIZE_SPECIAL_CHARS);
			$res = ($res)?$res:filter_input(INPUT_GET , $varName, FILTER_SANITIZE_SPECIAL_CHARS);
			return ($res==null)?"":trim($res);
		}
		
		public static function isEmail($email)
		{
		    return preg_match('|^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]{2,})+$|i', $email);
		}
		
		public static function combinePath($prefix,$postfix)
		{
			if($prefix[strlen($prefix)-1] == '/')
			{
				if($postfix[0] == '/')
				{
					$postfix = substr($postfix,1);
				}
			}
			else
			{
				if($postfix[0] != '/')
					$postfix = '/'.$postfix;
			}
			
			return $prefix.$postfix;
		}
		
		public static function getUserIP()
		{
			$config = Config::getInstance();
			
			$IP = "";
			
			$forceField = $config->get('forceIPField');
			
			if(isset($forceField) && strlen($forceField) > 0)
				$IP = $_SERVER[$forceField];
			else if(isset($_SERVER['REMOTE_ADDR']))
				$IP = $_SERVER['REMOTE_ADDR']; 
			else if (isset($_SERVER['X_FORWARDED_FOR'])) 
				$IP = $_SERVER['X_FORWARDED_FOR'];
			
			return $IP;
		}
		
	}
?>